package com.example.interview;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());

        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            private FirstFragment.onItemClickedListener onItemClickedListener;

            @NonNull
            @Override
            public Fragment getItem(int position) {
                RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        fab.setBackgroundTintList(ColorStateList.valueOf(Color.argb(255, recyclerView.computeVerticalScrollOffset() / 10 % 255, 0, recyclerView.computeVerticalScrollOffset() / 10 % 255 + 120)));
                    }
                };
                if (position == 0) {
                    return new FirstFragment(onScrollListener, item -> {
                        viewPager.setCurrentItem(1);
                        if (onItemClickedListener != null) {
                            onItemClickedListener.onItemClicked(item);
                        }
                    });
                } else {
                    SecondFragment secondFragment = new SecondFragment(onScrollListener);
                    onItemClickedListener = secondFragment;
                    return secondFragment;
                }
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return position == 0 ? "姓名" : "图片";
            }
        });

        TabLayout tabLayout = findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onBackPressed() {
        ViewPager viewPager = findViewById(R.id.viewPager);
        if (viewPager.getCurrentItem() > 0) {
            viewPager.setCurrentItem(0);
            return;
        }
        super.onBackPressed();
    }
}